package com.blensmile.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorInt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blensmile.BaseActivity;
import com.blensmile.R;
import com.blensmile.utils.ImageToast;
import com.blensmile.utils.LogUtils;
import com.blensmile.view.DrawView;
import com.blensmile.view.PlayVoiceView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Blensmile on 2016/11/14.
 */
public class MainActivity extends BaseActivity implements DrawView.ClickCallBack, View.OnClickListener {

    private static final String Tag = "A";
    private TextView tvShow;
    private static MyAdapter adapter = null;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                sendEmptyMessageDelayed(100, 1000);
                LogUtils.b("hello");
            }
        }
    };
    private TextView tvContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ListView list = (ListView) findViewById(R.id.listview);
        if (adapter == null) {
            adapter = new MyAdapter(this);
        }
        list.setAdapter(adapter);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!handler.hasMessages(100)) {
            handler.sendEmptyMessageDelayed(100, 1000);
        }
    }

    TranslateAnimation animation1;
    TranslateAnimation animation2;

    private void initView() {
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if(animation1 == null){
                    animation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_PARENT,0.5f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0);
                    animation1.setDuration(1000);
                }
                if(animation2 == null){
                     animation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_PARENT,0f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0);
                    animation2.setDuration(1000);
                }
                v.clearAnimation();
                v.startAnimation(animation1);
                ((TextView)v).setText("fuck you bitch");
                v.postOnAnimationDelayed(new Runnable() {
                    @Override
                    public void run() {
                        v.clearAnimation();
                        v.startAnimation(animation2);
                    }
                },1000);

            }
        });
    }

    class MyAdapter extends BaseAdapter {
        private Context context;
        private
        @ColorInt
        int[] colors = {0xffdceaf2, 0xfff4f3e0, 0xffdbf2ef, 0xfff4ecdb, 0xffdcf5e7, 0xfffaf0ef};
        private
        @ColorInt
        int[] ORDI = {0xfff1f8fc, 0xfffbfaee, 0xffe8faf7, 0xfff9f2e9, 0xffedfbf3, 0xfffbf5f4};

        public MyAdapter(Context context) {
            this.context = context;
            for (int i = 0; i < 100; i++) {
                list.add("");
            }
        }

        List<String> list = new ArrayList<>();

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
                holder = new Holder();
                holder.playVoiceView = (PlayVoiceView) convertView.findViewById(R.id.pv_voice);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.playVoiceView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtils.b("position:" + position + " --->id:" + v.hashCode());
                }
            });

            return convertView;
        }

        public class Holder {
            RelativeLayout rlWraper;
            ImageView imgSetting;
            PlayVoiceView playVoiceView;
        }
    }


    @Override
    public void onPlayClick() {
        ImageToast.showSingleToast("onPlayClick");
    }

    @Override
    public void onSettingClick() {
        ImageToast.showSingleToast("onSettingClick");
    }

    @Override
    public void onExitClick() {
        ImageToast.showSingleToast("onExitClick");
    }

    @Override
    public void onReportClick() {
        ImageToast.showSingleToast("onReportClick");
    }

    @Override
    public void onRelearnClick() {
        ImageToast.showSingleToast("onRelearnClick");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.b("resultCode : " + resultCode);
        LogUtils.b(data.toString());
    }
}
